package com.yidu.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * <p>@Description: No Description</p>
 * <p>@Author LiXin</p>
 * <p>@Date: 2021/6/22</p>
 * <p>@Time: 16:22</p>
 */
public class Tools {
    /**
     * 判断字符串是否为空
     * @param str
     * @return
     */
    public static boolean isEmpty(String str){
        if(str==null || "".equals(str)){
            return true ;
        }
        //不为空
        return false ;
    }

    /**
     * 将对象转换为json之后输出
     * @param response
     * @param obj
     * @throws IOException
     */
    public static void write(HttpServletResponse response,Object obj) throws IOException {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(obj);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.close();
    }


    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 将字符串转换为日期
     * @param str
     * @return
     */
    public static Date parseDateByStr(String str){
        Date date = null ;
        if(isEmpty(str)){
            return date ;
        }
        try {
            date= sdf.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }


    /**
     * 将日期转换为字符串
     * @param date
     * @return
     */
    public static String parseDate(Date date){
        if(date==null){
            return "";
        }else{
            return sdf.format(date);
        }
    }


    /**
     * 根据字符串转换为int
     * @param integer
     * @return
     */
    public static Integer parseIntByStr(String integer){
        if(isEmpty(integer)){
            return null ;
        }else{
            return Integer.valueOf(integer);
        }
    }

    /**
     * 根据字符串转换为double
     * @param d
     * @return
     */
    public static Double parseDoubleByStr(String d){
        if(isEmpty(d)){
            return null ;
        }else{
            return Double.valueOf(d);
        }
    }

    /**
     * 上传文件
     * @param request
     * @return
     * @throws IOException
     * @throws ServletException
     */
//    public static String uploadFile(HttpServletRequest request) throws IOException, ServletException {
//        Part part = request.getPart("Stu_Photo");
//        InputStream is = part.getInputStream();
//        String realPath = request.getServletContext().getRealPath("/upload");
//        String fileName = part.getSubmittedFileName();//获取文件名
//        String path = ""; //返回的路径
//        if(!Tools.isEmpty(fileName)){
//            System.out.println("===上传的文件名是:==="+fileName);
//            //取得后缀
//            String hz = fileName.substring(fileName.lastIndexOf("."));
//            String fileNameNews = UUID.randomUUID().toString()+hz;
//            OutputStream os = new FileOutputStream(realPath+"//"+fileNameNews);
//            byte[] bytes = new byte[1024];
//            int len = 0;
//            while((len=is.read(bytes))>-1){
//                os.write(bytes,0,len);
//            }
//            os.close();
//            is.close();
//            path = "/upload/"+fileNameNews;
//        }
//        return path ;
//    }
}
